<?php


namespace app\task\controller;

use app\api\model\Order as OrderModel;
use app\api\service\Bank;
use app\api\service\CashServiceJft;
use app\common\exception\BaseException;
use app\common\model\ChannelCcard;
use app\task\model\ChannelBcard;
use think\Log;

class Notifyjft extends \think\Controller
{
    public function bindcards(){
        $prama = $this->request->post();
//        $data = $prama['data'];
//        $signature = $prama['signature'];
//        $jft =new  CashServiceJft();
//        $res = $jft->decrypt($data);
        $res = $prama;
        //更改对应状态
        $status = $res['respCode'] == '0000'?1:0;
        if (!$status){
            Log::write('银行卡佳付通渠道激活失败 orderNo:'.$res['orderNo'] ,'yoshop-info');
        }
        $msg = $res['respMsg'];
        $channelbcard = ChannelBcard::get(['orderNo'=>$res['orderNo']]);
        if($channelbcard){
            $channelbcard->notes = $msg;
            $channelbcard->status = $status;
            $channelbcard->save();
        }else{
            Log::write('银行卡渠道激活失败 orderNo:'.$res['orderNo'].' 不存在对应记录 ','yoshop-info');
        }
    }
    public function bindccard(){
        $prama = $this->request->post();
//        $data = $prama['data'];
//        $signature = $prama['signature'];
//        $jft =new  CashServiceJft();
//        $res = $jft->decrypt($data);
        $res = $prama;
        //更改对应状态
        $status = $res['respCode'] == '0000'?1:0;
        if (!$status){
            Log::write('银行卡佳付通渠道激活失败 orderNo:'.$res['orderNo'] ,'yoshop-info');
        }
        $msg = $res['respMsg'];
        $channelccard = ChannelCcard::get(['orderNo'=>$res['orderNo']]);
        if($channelccard){
            $channelccard->notes = $msg;
            $channelccard->status = $status;
            if ($channelccard->save()){
                echo "";
            }
        }else{
            Log::write('银行卡渠道激活失败 orderNo:'.$res['orderNo'].' 不存在对应记录 ','yoshop-info');
        }
    }

    /**
     * 交易回调
     */
    public function pay_notifiy(){
        $prama = $this->request->post();
        $res = $prama;
        //更改对应状态
        $status = $res['resCode'] == '0000'?1:0;
        $order_no = $res['orderCode'];
        if ($status){
            // 获取商品结算信息
            $model = new OrderModel;
            if ($model->payOrder($order_no)) {
                $bank = new Bank();
                $bank->cash($order_no);
                return $this->renderJson('0000', '执行成功');
            } else {
                return $this->renderJson('0000', '找不到符合条件的订单');
            }
        }else{
            $model = OrderModel::getOrderDetailByOrderNo($order_no);
            if ($model->cancels()) {
                return $this->renderJson('0001','订单异常:'.$res['resMsg']);
            }
            return $this->renderJson('0002','订单异常:'.$res['resMsg']);
        }
    }

    /**
     * 提现回调
     */
    public function cash_notifiy(){
        //$prama = $this->request->post();
    }
    protected function renderJson($code = self::JSON_SUCCESS_STATUS, $msg = '', $data = [])
    {
        header('Access-Control-Allow-Origin: *');
        return compact('code', 'msg', 'data');
    }
}